package com.angel.activity;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.angel.entity.Task;
import com.angel.port.IActivity;
import com.angel.util.BaseUtil;
import com.angel.util.UserInfoUtil;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.NumberKeyListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class EmailBinding extends Activity implements IActivity{

	private Button email_back_Btn;
	private Button email_binding;
	//private Button cancel_email_binding;
	private Context context;
	private String email;
	private HashMap<String,String> map=new HashMap<String,String>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		 MobclickAgent.onError(this);//自动捕获异常退出（FC ）
		setContentView(R.layout.email_binding);
		context=EmailBinding.this;
		MyListener listenr = new MyListener();
		email_back_Btn = (Button)findViewById(R.id.email_back_bnt);
//		cancel_email_binding = (Button)findViewById(R.id.cancel_email_binding);
		email_binding = (Button)findViewById(R.id.email_binding);
		email_back_Btn.setOnClickListener(listenr);
		email_binding.setOnClickListener(listenr);
		//cancel_email_binding.setOnClickListener(listenr);
	}
    class MyListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			int id=v.getId();
			if(id==R.id.email_back_bnt){
				finish();
			}else if(id==R.id.email_binding){
				//建立窗口，设置EdtiText的属性
				final EditText edt = new EditText(EmailBinding.this);
				edt.setSingleLine(true);
				
				new AlertDialog.Builder(EmailBinding.this) 

				  .setTitle("请输入邮箱") 

				  .setIcon(R.drawable.mail_envelope) 

				  .setView(edt) 

				  .setPositiveButton("确定", new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						email=edt.getText().toString();
						map.put("email", email);
						map.put("userId", UserInfoUtil.getValue(context, "userId", ""));
						BaseUtil.startService(Task.EMAILBIND,EmailBinding.this,map);//启动登陆
					}
				  }) 

				  .setNegativeButton("取消", null) 

				  .show(); 
				
			}
//			else if(id==R.id.cancel_email_binding){
//			}
		}
    }


	@Override
	public void init() {
	}

	@Override
	public void refresh(Object... params) {
		String isSucess=BaseUtil.getValueFromJSON((String)params[0]);
		
		 if(isSucess.equals("2")){
			 BaseUtil.makeToast(EmailBinding.this, "对不起,网络连接超时,请稍后重试...");
			 return ;
		}
		    if(isSucess.equals("1")){//若绑定成功，则保存Email
				map=new HashMap<String,String>();
				map.put("email", email);
				UserInfoUtil.setValue(context, map);
			}else{
				BaseUtil.makeToast(this, "对不起你,绑定出错");
			}
		} 
	@Override
	protected void onResume() {
		super.onResume();
		 MobclickAgent.onResume(this);
	}
	@Override
	protected void onPause() {
		super.onPause();
		 MobclickAgent.onPause(this);
	}
}
